﻿/*
	Date:			05/25/2012
	Author:         Fabien Olousouzian
	License:      	Microsoft Public License (Ms-PL)
	Product:        XNA Shader Composer - Project
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;
using Shader_Composer.Primitive;

namespace Shader_Composer
{
    /// <summary>
    /// Display a simple 3D Model with his axis
    /// </summary>
    class DebugModel
    {
        #region Attributes

        private Model _model;
        private Vector3 _position;
        private float _rotation;
        private float _speedRotation;
        private Axis _axis;

        #endregion

        #region Methods

        /// <summary>
        /// Ctor
        /// </summary>
        public DebugModel()
        {
            _position = new Vector3();
            _rotation = MathHelper.ToRadians(0f);
            _axis = new Axis(7);
            _speedRotation = 4.6f;
        }

        /// <summary>
        /// Load the font
        /// </summary>
        /// <param name="content"></param>
        /// <param name="nameModel"></param>
        /// <param name="nameFontAxis"></param>
        public void Load(ContentManager content, string nameModel, string nameFontAxis)
        {
            _model = content.Load<Model>(nameModel);
            _axis.Load(content, nameFontAxis);
        }

        /// <summary>
        /// Update
        /// </summary>
        /// <param name="_device"></param>
        public void Update(ref Device _device)
        {
            _axis.Update(ref _device);

            if (_device.CurrentKeyboardState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Right))
            {
                this.RotationY -= MathHelper.ToRadians(_speedRotation);
                _axis.X.RotationAxisAngle(Line.Axe.Y, MathHelper.ToRadians(_speedRotation));
                _axis.Z.RotationAxisAngle(Line.Axe.Y, MathHelper.ToRadians(_speedRotation));
            }
            else if (_device.CurrentKeyboardState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Left))
            {
                this.RotationY += MathHelper.ToRadians(_speedRotation);
                _axis.X.RotationAxisAngle(Line.Axe.Y, -MathHelper.ToRadians(_speedRotation));
                _axis.Z.RotationAxisAngle(Line.Axe.Y, -MathHelper.ToRadians(_speedRotation));
            }

        }

        /// <summary>
        /// Draw the model
        /// </summary>
        /// <param name="camera"></param>
        /// <param name="beffect"></param>
        /// <param name="graphics"></param>
        public void DrawDebugModel(ref Camera camera, ref BasicEffect beffect, GraphicsDevice graphics)
        {
            //Draw Model
            Matrix[] transforms = new Matrix[_model.Bones.Count];
            _model.CopyAbsoluteBoneTransformsTo(transforms);

            foreach (ModelMesh mesh in _model.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.EnableDefaultLighting();
                    effect.World = transforms[mesh.ParentBone.Index] * Matrix.CreateRotationY(this.RotationY) * Matrix.CreateTranslation(_position); ;
                    effect.View = camera.ViewMatrix;
                    effect.Projection = camera.ProjectionMatrix;
                }
                mesh.Draw();
            }

            //Draw Axis
            _axis.Draw(ref beffect, graphics);
        }

        /// <summary>
        /// Update InfoDebug
        /// </summary>
        public void AddInfoDebug()
        {
            //_axis.AddInfoDebug();
            DisplayInfoDebug.Instance.Buffer += "Model - Position: " + _position + "\nRotationY: " + (int)MathHelper.ToDegrees(this.RotationY) + " degrees";
        }

        #endregion

        #region Getter & Setter

        public Model Model3D
        {
            get { return _model; }
            set { _model = value; }
        }

        public Vector3 Position
        {
            get { return _position; }
            set { _position = value;

            _axis.X.FirstPosition = new VertexPositionColor(value, Color.Blue);
            _axis.X.SecondPosition = new VertexPositionColor(new Vector3(_axis.X.SecondPosition.Position.X, value.Y, value.Z), Color.Blue);

            //_axis.Y.FirstPosition = new VertexPositionColor(value, Color.Red);
            //_axis.Y.SecondPosition = new VertexPositionColor(new Vector3(0, value.Y, 0), Color.Red);

            //_axis.Z.FirstPosition = new VertexPositionColor(value, Color.Green);
            //_axis.Z.SecondPosition = new VertexPositionColor(new Vector3(0, 0, value.Z), Color.Green);
            
            }
        }

        public float RotationY
        {
            get { return _rotation; }
            set
            {
                if (MathHelper.ToDegrees(_rotation) <= -360)
                    _rotation += MathHelper.ToRadians(360);
                else if (MathHelper.ToDegrees(_rotation) >= 360)
                    _rotation -= MathHelper.ToRadians(360);
                else
                    _rotation = value;
            }
        }

        #endregion

    }
}
